#include "StdAfx.h"
#include "DllContextMenu.h"

const GUID CLSID_DllContextMenu    = {0xE983CEC3,0xDEA7,0x4604,{0x85,0xD0,0x70,0xD9,0x61,0x2E,0x9D,0x58}};
const GUID LIBID_DllContextMenuLib = {0x0F4A43FC,0x1F3D,0x4DDC,{0xBA,0x5D,0xF2,0x1D,0x96,0x64,0x4E,0x83}};

CDllContextMenuModule _AtlModule;

CApprovedExtension ApprovedExtension()
{
	return CApprovedExtension(ProjectName, CLSID_DllContextMenu);
}

EXTERN_C BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	if (dwReason == DLL_PROCESS_ATTACH)
	{
		DisableThreadLibraryCalls(hInstance);
	}

	return _AtlModule.DllMain(dwReason, lpReserved); 
}

STDAPI DllCanUnloadNow()
{
    return _AtlModule.DllCanUnloadNow();
}

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}

STDAPI DllRegisterServer()
{
	HRESULT hr = ApprovedExtension().Install();

	if (FAILED(hr))
	{
		return hr;
	}

    return _AtlModule.DllRegisterServer(FALSE);
}

STDAPI DllUnregisterServer()
{
	HRESULT hr = ApprovedExtension().Uninstall();

	if (FAILED(hr))
	{
		return hr;
	}

	return _AtlModule.DllUnregisterServer(FALSE);
}

STDAPI DllInstall(BOOL bInstall, LPCWSTR)
{
	HRESULT hr;

    if (bInstall)
    {	
    	if (FAILED(hr = DllRegisterServer()))
    	{	
    		DllUnregisterServer();
    	}
    }
    else
    {
    	hr = DllUnregisterServer();
    }

    return hr;
}